#include "FichierDial.h"

FichierDial::FichierDial()
{
    fichier.open("dials2", ios_base::in|ios_base::out|ios_base::app);
    if(fichier.fail())
    {
        cout << "Erreur!!"<< endl;
    }
    fichier.seekg(ios_base::beg);
    fichier.seekp(ios_base::end);
    imgScreenDial.LoadFromFile("Images/Interface/dial2.bmp");
    screenDial.SetImage(imgScreenDial);
    pnjTalk = true;
    maxDial = 1;
    numDial = 0;
    if (!MyFont.LoadFromFile("font.ttf", 24))
    {
        cout << "Echec loading font"<<endl;
    }                                        ;

}
FichierDial::~FichierDial()
{
    fichier.close();
}


bool FichierDial::isEnd()
{
    bool end = fichier.peek()==EOF;
    if(fichier.eof())
    {
        fichier.clear();
    }
    return end;

}

int FichierDial::searchDial(string n, string s, string scene)
{
    pnjTalk = true;
    numDial = 0;
    fichier.seekg(ios_base::beg);
    bool peek = false;
    string t, name, surname;
    string dialogue;
    while(not isEnd() and !peek)
    {

        getline(fichier, t);
        list->searchName(t, name, surname);
        string temp;
        getline(fichier, temp);
        if(temp != "#begin")
        {
            cout << "Erreur lecture fichier : #begin attendu pour " << name << " " << surname;
            exit(0);
        }
        bool endT = false;
        peek = (n == name && s == surname);
        while (!endT)
        {
            getline(fichier, temp);
            if(temp != "#end")
            {
                 if( temp == scene and peek)
                {
                    endT = true;
                }
                getline(fichier, temp);
                maxDial = atoi(temp.data());
                text = new string[maxDial];
                for(int i=0; i<maxDial; i++)
                {
                    getline(fichier, temp);
                    text[i] = temp;
                }

            }
            else
            {
                endT = true;
            }
        }

    }
    if(!peek)
    {
        cout << "Erreur dialogue"<<endl;
    }
    return peek;

}

string FichierDial::readLine()
{
    string l;
    string temp = "";
    while(temp.size()<40 and text[numDial].size()>0)
    {
        temp += readWord();
        temp += ' ';
    }
    return temp;
}

string FichierDial::readWord()
{
    int i = 0;
    char c = text[numDial][i];
    string s = "";
    while(c != ' ' and c != '\0' and c != ',' and c != '.' and c != ':' and c != ';')
    {
        s += c;
        i++;
        c = text[numDial][i];
    }
    text[numDial].erase(0, i+1);
    if(list->isCode(s))
    {
        if(s == "%name" )
        {
            s = perso->getName();
        }
        else if(s == "%surname")
        {
            s = perso->getSurname();
        }
        else
        {
            string n,  m;
            list->searchName(s, n, m);
            s = m + " " + n;
        }

    }
    if(  c == ',' or c == '.' or c == ':' or c == ';' )
            s += c;

    return s;
}

bool FichierDial::endRead()
{
    return text[maxDial-1].size()==0;
}

void FichierDial::drawDial()
{
    draw();
    screen->getRenderWindow().Display();
    float x = perso->getPositionX() - (MAIN_WIDTH/2);
    float y = perso->getPositionY() + (MAIN_HEIGHT/2)-screenDial.GetSize().y;
    int i = 1;
    int pos = 0;
    Clock horloge;
    String l1, l2, l3, l4;
    while(!endRead() and i<5 and !endLine())
    {
        string s1 = readLine();
        String s;
        pos = 0;
        while(pos < s1.size())
        {
            horloge.Reset();
            float fr = 0.0;
            while(fr < 0.008f)
            {
                fr = horloge.GetElapsedTime();
            }
            s.SetText(s1.substr(0, pos));
            s.SetPosition(x + 220, y + ((i+1)*32));
            s.SetFont(MyFont);
            s.SetSize(24);

            draw();
            screen->getRenderWindow().Draw(s);
            screen->getRenderWindow().Draw(l1); screen->getRenderWindow().Draw(l2); screen->getRenderWindow().Draw(l3); screen->getRenderWindow().Draw(l4);
            screen->getRenderWindow().Display();
            pos++;
        }
        switch(i)
            {
                case 1: l1 = s;
                case 2 : l2 = s;
                case 3 : l3 = s;
                case 4 : l4 = s;
            }
        i++;
    draw();
    screen->getRenderWindow().Draw(l1); screen->getRenderWindow().Draw(l2); screen->getRenderWindow().Draw(l3); screen->getRenderWindow().Draw(l4);
    screen->getRenderWindow().Display();
    }
     if(endLine())
    {
        numDial++;
        pnjTalk = !pnjTalk;
    }


}

bool FichierDial::endLine()
{
    return text[numDial].size()==0 ;
}

void FichierDial::setScreen(Screen* s)
{
    screen = s;
}

void FichierDial::renderDialogue(Pnj* p, Fighter* per, string scene)
{
    pnj = p;
    perso = per;
    bool d = false;
    searchDial(p->getName(), p->getSurname(), scene);
    screen->drawWorld();
    drawDial();
    screen->display();

    while(not d)
    {
        screen->getRenderWindow().GetEvent(event);
        if((event.Type == sf::Event::KeyPressed) && (event.Key.Code == sf::Key::Z))
        {
            if(endRead())
            {
                screen->drawWorld();
                d = true;
            }
            else
            {
                drawDial();
                screen->display();
            }
        }
    }
}


void FichierDial::setList(NameList* l)
{
    list = l;
}

void FichierDial::draw()
{
    float x = perso->getPositionX() - (MAIN_WIDTH/2);
    float y = perso->getPositionY() + (MAIN_HEIGHT/2)-screenDial.GetSize().y;
    screenDial.SetPosition(x,y);
    screen->getRenderWindow().Draw(screenDial);
    String n;
    n.SetFont(MyFont);
    n.SetPosition(x + 220, y+12);
    if(pnjTalk)
    {
        pnj->setHeadPosition((int) (x+10+((screenDial.GetSize().x-MAIN_WIDTH)/2.0)),(int)((y+(screenDial.GetSize().y-200)/2.0)));
        screen->getRenderWindow().Draw(*pnj->getPnjHead());
        n.SetText(pnj->getSurname() + " " + pnj->getName());
    }
    else
    {
        perso->setHeadPosition((int) (x+10+((screenDial.GetSize().x-MAIN_WIDTH)/2.0)),(int)((y+(screenDial.GetSize().y-200)/2.0)));
        screen->getRenderWindow().Draw(*perso->getPnjHead());
        n.SetText(perso->getSurname() + " " + perso->getName());
    }
     screen->getRenderWindow().Draw(n);
}
